package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.StringUtils;
import com.mingtu.guards.R;
import com.mingtu.guards.bean.EcDequiBean;

import java.util.HashMap;
import java.util.List;


/**
 * 描述：
 * 修改人：
 * 修改时间：
 * 修改备注：
 *
 * @author wkk
 */

public class ScreenControlAdapter1 extends RecyclerView.Adapter<ScreenControlAdapter1.ActivityHolder> implements View.OnClickListener {
    private Context context;
    private List<EcDequiBean.DataBean> mData;
    private OnItemClickListener mOnItemClickListener = null;
    private HashMap<Integer, Boolean> statusMap = new HashMap<>();

    private boolean isTable = com.mingtu.common.utils.ScreenUtils.isTablet();

    public ScreenControlAdapter1(Context context, List<EcDequiBean.DataBean> mData) {
        this.context = context;
        this.mData = mData;
    }

    @Override
    public ActivityHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_screen_control, parent, false);
        // 实例化viewholder
        ActivityHolder viewHolder = new ActivityHolder(view, isTable);
        viewHolder.itemView.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ActivityHolder holder, int position) {
        if (mData != null) {
            if (mData.size() > 0) {
                EcDequiBean.DataBean dataBean = mData.get(position);
                String dequName = dataBean.getName();
                holder.tvTargeName.setText(!StringUtils.isEmpty(dequName) ? dequName : "");
                boolean status = statusMap.get(position);
                if (status) {
                    holder.layout.setBackground(context.getDrawable(R.drawable.shape_bg_15_gradient));
                    holder.ivFlag.setImageResource(R.mipmap.icon_screen_control_flag2);
                    holder.tvTargeName.setTextColor(context.getResources().getColor(R.color.color_white));
                } else {
                    holder.layout.setBackground(context.getDrawable(R.drawable.shape_bg_15_5ab_stroke));
                    holder.ivFlag.setImageResource(R.mipmap.icon_screen_control_flag1);
                    holder.tvTargeName.setTextColor(context.getResources().getColor(R.color.text_color_5ab));
                }
            }
        }

        //将position保存在itemView的Tag中，以便点击时进行获取
        holder.itemView.setTag(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    public void upData(List<EcDequiBean.DataBean> data) {
        mData = data;
        for (int i = 0; i < mData.size(); i++) {
            statusMap.put(i, false);
        }
        notifyDataSetChanged();
    }

    public void upData(int position) {
        for (int i = 0; i < mData.size(); i++) {
            statusMap.put(i, false);
        }
        statusMap.put(position, true);
        notifyDataSetChanged();
    }

    public void resetData() {
        for (int i = 0; i < mData.size(); i++) {
            statusMap.put(i, false);
        }
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public static class ActivityHolder extends RecyclerView.ViewHolder {
        private final View convertView;
        public LinearLayout layout;
        public ImageView ivFlag;
        public TextView tvTargeName;

        public ActivityHolder(View itemView, boolean isTable) {
            super(itemView);
            convertView = itemView;
            layout = (LinearLayout) convertView.findViewById(R.id.layout);
            ivFlag = (ImageView) convertView.findViewById(R.id.iv_flag);
            tvTargeName = (TextView) convertView.findViewById(R.id.tv_targe_name);


//            ViewGroup.LayoutParams lp = layout.getLayoutParams();
//            int screenWidth = ScreenUtils.getScreenWidth(); //照片一行3个，间隙是20dp
//
//            int mWidth = (screenWidth - SizeUtils.dp2px(15)) / 2;
//            int mHeight = mWidth * SizeUtils.dp2px(73) / SizeUtils.dp2px(165);
//            if (isTable) {
//                mWidth = (screenWidth - SizeUtils.dp2px(15)) / 3;
//            }
//
//            lp.width = mWidth;
//            lp.height = mHeight;
//            layout.setLayoutParams(lp);
        }
    }

}
